package com.supostacks.infrastructure.repository;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.supostacks.domain.repository.IFeedbackRepository;
import com.supostacks.domain.vo.FeedbackVo;
import com.supostacks.infrastructure.context.DefaultContext;
import com.supostacks.infrastructure.context.EntityUtil;
import com.supostacks.infrastructure.dao.FeedbackDao;
import com.supostacks.infrastructure.dao.FeedbackDao;
import com.supostacks.infrastructure.po.Feedback;
import com.supostacks.infrastructure.po.Feedback;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class FeedbackRepository extends DefaultContext implements IFeedbackRepository {

    @Resource
    private FeedbackDao feedbackDao;

    @Override
    public String insert(FeedbackVo feedbackVo) {
        try {
            Feedback feedback = new Feedback();
            EntityUtil.copyFields(feedbackVo, feedback);

            int insert = feedbackDao.insert(feedback);
            if (insert > 0) {
                return feedback.getId();
            }
            return null;

        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void deleteById(String id) {
        feedbackDao.deleteById(id);
    }

    @Override
    public String updateById(FeedbackVo feedbackVo) {
        try {
            Feedback feedback = new Feedback();
            EntityUtil.copyFields(feedbackVo, feedback);
            int i = feedbackDao.updateById(feedback);
            if (i > 0) {
                return feedback.getId();
            }
            return "";
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public FeedbackVo selectById(String id) {
        try {
            Feedback feedback = feedbackDao.selectById(id);
            if (feedback != null) {
                FeedbackVo feedbackVo = new FeedbackVo();
                EntityUtil.copyFields(feedback, FeedbackVo.class);
                return feedbackVo;
            }
            return null;
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<FeedbackVo> selectList(JSONObject param) {
        QueryWrapper<Feedback> queryWrapper = where(param);
        List<Feedback> feedbacks = feedbackDao.selectList(queryWrapper);
        return buildList(feedbacks);
    }

    @Override
    public JSONObject selectByPage(JSONObject options, int pageNo, int pageSize) {
        return null;
    }

    public List<FeedbackVo> buildList(List<Feedback> feedbacks) {
        return feedbacks.stream().map(feedback -> {
            try {
                FeedbackVo feedbackVo = new FeedbackVo();
                EntityUtil.copyFields(feedback, feedbackVo);
                return feedbackVo;
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        }).collect(Collectors.toList());
    }
}
